﻿using System;
using System.Collections.Generic;

namespace Test30.Engine
{
    public class ChooseDrop<T> : CollectionDropBase<T>, IDrop<T>
    {
        public void FixOdds()
        {
            foreach (DropChoice choice in Choices)
            {
                // give it even odds out of the total
                if (Math.Abs(choice.Odds) < 1e-8) choice.Odds = TotalOdds / Choices.Count;
            }
        }

        #region IDrop Members

        public IEnumerable<T> Create(int level)
        {
            // use the actual total in case the data adds up to more than 100
            float choiceValue = Rng.Float(TotalOdds);

            IDrop<T> dropped = null;

            // count through to find the chosen one
            foreach (DropChoice choice in Choices)
            {
                choiceValue -= choice.Odds;
                if (choiceValue <= 0)
                {
                    dropped = choice.Drop;
                    break;
                }
            }

            // drop it if we have one
            if (dropped != null)
            {
                foreach (var item in dropped.Create(level))
                {
                    yield return item;
                }
            }
        }

        #endregion
    }

}
